﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

namespace ebookstore
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                loginPanel.Visible = false;
                infoLabel.Text = "You are logged in "+Session["user"];
            }
        }
        protected void signIn(object sender, EventArgs e)
        {
            if (usernameTextBox.Text == "hades" && passwordTextBox.Text == "1595")
            {
                Session["user"] = usernameTextBox.Text;
                Response.Redirect("Default.aspx");
            }
            else if (usernameTextBox.Text == "admin" && passwordTextBox.Text == "admin")
            {
                Session["user"] = usernameTextBox.Text;
                Response.Redirect("Default.aspx");
            }
            else
            {
                infoLabel.ForeColor = System.Drawing.Color.Red;
                infoLabel.Text = "Wrong username or password";
            }
        }
        protected void signUp(object sender, EventArgs e)
        {
            Response.Redirect("RegisterUser.aspx");
        }

        protected void forgotPasswordLinkButton_Click(object sender, EventArgs e)
        {
            infoLabel.Visible = false;
            loginPanel.Visible = false;
            emailSendPanel.Visible = true;
        }

        protected void sendEmailButton_Click(object sender, EventArgs e)
        {
            try
            {
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.Port = int.Parse("587");
                smtpClient.EnableSsl = true;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new System.Net.NetworkCredential("karhadestest@gmail.com", "qmkda1595");
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                
                MailMessage mail = new MailMessage();
                mail.Subject = "Hades rocks!";
                mail.From = new MailAddress("karhadestest@gmail.com","Takis");
                mail.To.Add(new MailAddress(sendEmailTextBox.Text));
                mail.Body = "Your password is: ";
                mail.IsBodyHtml = true;

                smtpClient.Send(mail);
                sendEmailInfoLabel.Text = "Email sent successfully.";
            }
            catch (Exception ex)
            {
                sendEmailInfoLabel.Text = ex.Message;
            }
        }
    }
}